	var numberOfPictures= 5;


function StartCapture(mode,tagarray)
{
	if( mode == "still")
		StartCaptureStill(tagarray);
	
	if( mode == "timelapse")
		StartCaptureTime(tagarray);
		
	if( mode == "group")
		StartCaptureGroup(tagaray);
		
}

function setCount( t)
{
//	for ( a in t) printf("*" , a);
	
	numberOfPictures = t.value;

}

/*
 * random number n:  1 <= n  <=  y
 * if y= 1 return 1
 * if y= 2 return 1,2
 * if y= 3 return 1,2,3
 * if y =n return 1,2,3,4,...n
 */
 
function randomx(y )
{
	var r;
	var z2 = new Array(1,2,1,2,1,2,1,2,1,2);
	var z3 = new Array(1,2,3,1,2,3,1,2,3,1,2,3);
	var z4 = new Array(1,2,3,4,1,2,3,4,1,2,3,4);
	var z5 = new Array(1,2,3,4,5,1,2,3,4,5);
	var z6 = new Array(1,2,3,4,5,6,1,2,3,4,5,6);
	var z7 = new Array(1,2,3,4,5,6,7,1,2,3,4,5,6,7);
	

	if(y>7) Alert("error in randomx, increase array size");
	
	if(y==1) return 1;
	else if (y==2) {shuffle(z2); return z2[0];}
	else if (y==3) {shuffle(z3); return z3[0];}
	else if (y==4) {shuffle(z4); return z4[0];}
	else if (y==5) {shuffle(z5); return z5[0];}
	else if (y==6) {shuffle(z6); return z6[0];}
	else if (y==7) {shuffle(z7); return z7[0];}
	


}


 	var uWhiteBalance, sWhiteBalance;		// White Balance
	var uStrobeMode, sStrobeMode;			// Strobe Mode
	var uStillQuality, sStillQuality;				// Still Quality
	var uStillResolution, sStillResolution;			// Still Resolution
	var uBurstQuality, sBurstQuality;				// Burst Quality
	var uBurstResolution, sBurstResolution;			// Burst Resolution
	var uTimeQuality, sTimeQuality;					// Time Quality
	var uTimeResolution, sTimeResolution;			// Time Resolution

	var stringColor;
	var stringXPosition;
	var stringBackGroundColor;
	var stringYPosition;
	
	var timeColor;
	var timeXPosition;
	var timeBackGroundColor;	
	var timeYPosition;

	
/*
 * accumulateWaterMark() represents a compilation of the capture settings
 * each time this function is called a global is updated
 * use displayWaterMarkMark() to display the coded text string
 */

function accumulateWaterMark( theParam	,theType , theActualValue )	
{
	
	// White Balance
	if( theParam.param == '"wmod"') sWhiteBalance= keyWhiteBalance[theType];
             
	// Strobe Mode
	if( theParam.param == '"smod"') sStrobeMode = keyStrobe[theType];


	// Still Quality, Resolution
	if( theParam.param == '"scpn"') sStillQuality = keyQuality[theType];
	if( theParam.param == '"ssiz"') sStillResolution = keyResolution[theType];
	// Burst
	if( theParam.param == '"bcpn"') sBurstQuality  = keyQuality[theType];
	if( theParam.param == '"bsiz"') sBurstResolution = keyResolution[theType];
	// Time lapse Quality, Resolution
	if( theParam.param == '"tcpn"') sTimeQuality = keyQuality[theType];
	if( theParam.param == '"tsiz"') sTimeResolution = keyResolution[theType];

	if( theParam.param == '"wsbc"') stringBackGroundColor=keyColor[theActualValue]; 
	if( theParam.param == '"wsfc"') stringColor=keyColor[theActualValue];  

	if( theParam.param == '"wtbc"') timeBackGroundColor=keyColor[theActualValue]; 
	if( theParam.param == '"wtfc"') timeColor=keyColor[theActualValue];  
	
	if( theParam.param == '"wsxp"')	stringXPosition = theActualValue;
	if( theParam.param == '"wsyp"') stringYPosition = theActualValue;

	if( theParam.param == '"wtxp"')	timeXPosition = theActualValue;
	if( theParam.param == '"wtyp"') timeYPosition = theActualValue;
			

}


function displayWaterMark(thisCount)
{
    var theWaterMark;
    
    theWaterMark='"';

	theWaterMark += thisCount;
    theWaterMark += sWhiteBalance; 
    theWaterMark += sStrobeMode;   		
	theWaterMark += sStillQuality;
	theWaterMark += sStillResolution;
	theWaterMark += sBurstQuality;
	theWaterMark += sBurstResolution;
	// have to fit in a frame rate here?
	
	theWaterMark += sTimeQuality;
	theWaterMark += sTimeResolution;
	theWaterMark += stringColor;
	theWaterMark += stringXPosition;
	theWaterMark += stringBackGroundColor;
	theWaterMark += stringYPosition;
	theWaterMark += timeColor;
	theWaterMark += timeXPosition;
	theWaterMark += timeBackGroundColor;	
	theWaterMark += timeYPosition;
	theWaterMark += '"';
	
	return theWaterMark;
	
}

function setup( a,theGuys)
{
	 var i,commonItems,extraItems,kickout,nthItem,param,uval,sval,uran;


	// ** number of parameter tags in the tag object (common to all)
	commonItems=0;
	for(prop in parameterTag)  commonItems++;

	// set all parameters relevent to picture taking			    			
	for(i=0  ; i< theGuys.length ; i++  ){
				
			
		// count number of extra items in this parameter tag
		extraItems=0;
		for(prop in a[theGuys[i]] )   extraItems++;

		// a random value between 1 and the # of extra items
		kickout = randomx(extraItems-commonItems);
		
		
		// Enum type (pick just one) *******************************
		// index through again but kick out at the nth Item
		// this effectively picks a random item of the Enum List
		nthItem=0;
		if(  a[theGuys[i]].type.substring(0,4) == "Enum" )
		{
			for(theType in a[theGuys[i]] ){
				
				if( paramNotThere( theType , parameterTag) ){
						nthItem++;
						if(nthItem == kickout) break;
				}
			} 

		// only do this if something found in Enum List
			if(nthItem)
			{
  				param="param"+i;
				sval="sval"+i;
  				uval="uval"+i;	
 				printf(param,' = ', a[ theGuys[i] ].param  );
				printf(sval,' = ','"',theType,'"');
 				printf(uval,' = ',  a[ theGuys[i] ][theType]  );	
				accumulateWaterMark( a[ theGuys[i] ], theType ,a[ theGuys[i] ][theType]);	
			}
		}// end of Enum List type
		
		
		
		// Min-Max type take a value in between  min and max *********
  			param="param"+i;
 			uval = "uval"+i;
		if( a[theGuys[i]].max != 0 )
		{
 			printf(param,' = ', a[ theGuys[i] ].param  );
 			randomValue =  randomlowhigh(a[theGuys[i]].min , a[theGuys[i]].max )
 			printf(uval,' = ',  randomValue );
 			accumulateWaterMark( a[ theGuys[i] ], a[theGuys[i]].param ,  randomValue);				
 									
		}
				
	
		printf(' ');
  }
   
}

function declarations(theKind,theParam, limit)
{

	var start,end;
	
	start=0;
	end=5;
	
	while(start <= limit)
	{
			print(  'declare ' + theKind + ': ');
			for( i = start; i < end ; i++)
					print(theParam+i,',' );
	
			printf(theParam+i );
			
			start=end+1;
			end = start+5;
	
	}




}
function StartCaptureStill( a )
{
	flashpoint();
	
	/* variable parameters
	 * "smod"     flash
	 * "scpn"	   quality
	   "ssiz"      resolution
	   "sccl"      color
	   "bcpn"      burst 
	   "bsiz"      
	 */
	 
	var theGuys = new Array("wmod", "smod","scpn","ssiz","bcpn","bsiz","tcpn","tsiz","wsfc", "wsxp","wsbc", "wsyp","wtfc","wtxp","wtbc","wtyp");


	// some explanatory text
	printf('# these parameter settings are randomly changed each time  ' );
	printf('# this program is generated ');
	printf('# a watermark describes the settings ');
	printf('# use the edit box to set the number of images taken');
	
	printf('# key code is identical to those generated from RNRs filemaker program');
	for(i=0 ; i< theGuys.length ; i++) {		
  		printf('# ',a[ theGuys[i] ].param ,' *** ',a[ theGuys[i] ].desc , ' ***'); 					
	}

	writeMenu('"StartCaptureStill"' , '"EXTRA"','"StC(still-random)"');
	writeVariables();
	printf('# StartCapture parameters ');
	printf('declare u: imageCount, timerDelay, captureSize ');
	printf('declare u: startCount,autoCount' );
	printf('declare s: groupName' );

	printf('declare s: watermark');
	
	declarations("s", "param", theGuys.length );
	declarations("u", "umid", theGuys.length );
	declarations("u", "uval", theGuys.length );
	declarations("s", "sval", theGuys.length );
	declarations("s", "title", theGuys.length );

	printf('<br>','#**** OPEN FILE ****');
  	printf('outFile="/System/ZStill.txt"');
	writeFileOpen();

	for(i=0 ; i< theGuys.length ; i++) 		
  		printf("title"+i,' = " ', a[ theGuys[i] ].desc,' "');


	printf(' ');	
	printf('count=0');
//	var numberOfPictures= 5;
	printf('imageCount = ',numberOfPictures);
	printf('SetCaptureMode(still) ');
	printf('TAKEANOTHERPICTURE:');
	printf('count=count+1');

	for(iteration=1; iteration<=numberOfPictures ; iteration++){
		printf('<br>if count == ' , iteration);
		setup( a, theGuys);
		printf('watermark = ', displayWaterMark(iteration) );

//		displayWaterMark();
		printf('end ');
	}
	
	printf('<br># SETTINGS ***** ');
	for(i=0 ; i<theGuys.length ; i++){
		uval="uval"+i;
		printf('SetCameraState(',a[theGuys[i]].param,', ',uval,' )' );
	}
	    printf('SetCameraState("wena",7)');
		printf('SetCameraState("wstr",watermark)');
		
	printf(' ');
	printf(' # take pictures  ****************************************');
	printf(' FileClose( fd )');
	printf(' FileOpen( 2, outFile , 1, fd ) ');
	printf(' ');
	
	printf(' status=StartCapture() ');

	printf(' <br># *****************************');
	printf(' WAITFOR_IPIP:');
	printf(' GetCameraStatus(system,capture,vendor) ');
	printf(' Display(".")');
	printf(' if system & 0x10000000 ');
	printf(' Wait(5000) ' );
	printf(' goto WAITFOR_IPIP ' );
	printf(' end');
	printf(' DisplayClear() ');
	printf(' # *****************************<br>');

	printf(' GetTimeString(time) ');
	printf(' Write(fd,count,") ", time," " ) ');
	
	printf(' DisplayLine("StCapt[",status,"] (",count,")" ) ');
	printf(' WriteLine(fd,"StCapt[",status,"] ") ');

	printf(' if status == 0 ');
	for(i=0 ; i<theGuys.length ; i++){
		param="param"+i;
		uval="uval"+i;
		sval="sval"+i;
		stitle="title"+i;
		printf('WriteLine(fd,',param,',"  ",',sval,',"  ",',uval,',',stitle,')' );
	}
		printf('WriteLine(fd, watermark )');
	printf(' end');
	
	printf(' if status != 0 ');
	printf(' WriteLine(fd, "PICTURE NOT TAKEN ") ');
	printf(' end');

	printf(' Wait(100) ');
	printf(' if count < imageCount');
	printf(' goto TAKEANOTHERPICTURE');
	printf(' end');

	printf('');
	printf('# FINISHED PICTURES, call EndCapture **********');
	printf(' EndCapture()');


	printf(' FileClose( fd )');
	
	printf(' # uncomment to call "erase.csm" and  erase files******* ');	
	printf('# RunScript("erase.csm")');
//	printf(' end');



}

function StartCaptureTime()
{

	writeMenu('"StartCapture(tlap)"' , '"EXTRA"','"StartCapture(tlap)"');
	writeVariables();
	printf('# StartCapture parameters ');
	printf('declare u: imageCount, timerDelay, captureSize ');
	printf('declare u: startCount,autoCount' );
	printf('declare s: groupName' );

	printf('<br>','#**** OPEN FILE ****');
  	printf('outFile="/System/ZTime.txt"');
	writeFileOpen();
		
	
	printf('# takes time lapse pictures  ');
	printf('# when done call "RunScript("erase.csm") to erase pictures ');
	printf('# to vary number of pictures, change "imageCount" line ');
	printf('# to vary elapsed time between shots, change timerDelay');

	printf(' imageCount = 15 ');
	printf(' timerDelay = 60 ');
	printf(' captureSize = 1 ');
	
	printf(' # take pictures  *********************************');

	printf(' # set capture mode to time lapse ');
	printf(' SetCaptureMode(tlap) ');

	printf('<br>');	
	printf(' WriteLine(fd,"about to take ",imageCount," time lapse pictures") ');
	printf(' WriteLine(fd,timerDelay," secs apart ",captureSize," captureSize") ');
		
	printf(' status=StartCapture(imageCount,timerDelay,captureSize) ');

	printf(' GetTimeString(time) ');	

	printf(' if status == 0 ');
	printf(' WriteLine(fd,time,"StCapt[",status,"] ",imageCount," pictures taken " ) ');
	printf(' end');
	
	printf(' if status != 0 ');
	printf(' WriteLine(fd,time, "PICTURES NOT TAKEN ") ');
	printf(' end');

	printf('');
	printf('# FINISHED PICTURES, call EndCapture **********');
	printf(' EndCapture()');


	printf(' FileClose( fd )');
	
	printf(' ');
	printf('# uncomment to call "erase.csm" ******* ');	
	printf('# insert a RunScript("---" ) in "erase.csm" and call this file ');
	printf('# RunScript("erase.csm")');


}


function StartCaptureGroup()
{
	flashpoint();


	writeMenu('"StartCapture(group)"' , '"EXTRA"','"StartCapture(group)"');
	writeVariables();
	printf('# StartCapture parameters ');
	printf('declare u: imageCount, timerDelay, captureSize ');
	printf('declare u: startCount,autoCount' );
	printf('declare s: groupName' );

	printf('<br>','#**** OPEN FILE ****');
  	printf('outFile="/System/ZGroup.txt"');
	writeFileOpen();

	printf('# takes group pictures  ');
	printf('# when done call "RunScript("erase.csm") to erase pictures ');
	printf('# to vary number of pictures, change "imageCount" value ');	
	printf('# startCount = "grct" param of first picture ');
	printf('# sucessive shots increment by 1 if autoCount is non-zero ');
	printf(' ');
	printf(' imageCount = 5 ');	
	printf(' groupName = "FLASHPNT" ');
	printf(' startCount = 10 ');
	printf(' autoCount = 1 ');
	
	printf(' # take pictures  *********************************');

	printf(' count = 0 ');
	printf(' # only set group mode ONCE ');
	printf(' SetCaptureMode(group) ');
	printf(' status=StartCapture(groupName,startCount,autoCount) ');

	printf(' KEEPONGOING: ');
	printf(' FileClose( fd ) ');
	printf(' FileOpen( 2, outFile , 1, fd ) ');

	printf(' GetTimeString(time) ');
	printf(' Write(fd,time,"[",count,"]:") ');

	printf(' # ***************************** ');
	printf(' WAITFOR_IPIP: ');
	printf(' GetCameraStatus(system,capture,vendor) ');
	printf(' Display(".") ');
	printf(' if system & 0x10000000 ');
	printf(' Wait(5000) ');
	printf(' goto WAITFOR_IPIP ');
	printf(' end ');
	printf(' DisplayClear() ');
	printf(' # ***************************** ');
	printf(' ');

	printf(' status=StartCapture() ');
	printf(' DisplayLine("StCapt[",status,"] (",count,")" ) ');

	printf(' if status==0 ');
	printf(' Write(fd,"StCapt[",status,"] ") ');
	printf(' WriteLine(fd, count," pictures taken ") ');
	printf(' count = count + 1 ');
	printf(' end ');

	printf(' if count < imageCount ');
	printf(' goto KEEPONGOING ');
	printf(' end ');


	printf('');
	printf('# FINISHED PICTURES, call EndCapture **********');
	printf(' EndCapture()');

	printf(' FileClose( fd )');
	
	printf(' ');
	printf('# uncomment to call "erase.csm" ******* ');	
	printf('# insert a RunScript("---" ) in "erase.csm" and call this file ');
	printf('# RunScript("erase.csm")');
	printf('exitscript');

}

